package reference;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author hujian
 * @date 2023/2/15 10:02
 * @description
 */
public class ReferenceChecker {
    public static void main(String[] args) throws IOException {
        String reference = "Mutinda James,Mwangi Waweru & Okeyo George.(2023).Sentiment Analysis of Text Reviews Using Lexicon-Enhanced Bert Embedding (LeBERT) Model with Convolutional Neural Network. Applied Sciences(3). doi:10.3390/APP13031445.";

        Properties properties = new Properties();
        properties.load(new FileInputStream("src/main/resources/apa.properties"));

        boolean isMatch = checkAPAFormat(reference, properties);

        if (isMatch) {
            System.out.println("This reference is written in APA format.");
        } else {
            System.out.println("This reference is not written in APA format.");
        }
    }

    public static boolean checkAPAFormat(String reference, Properties properties) {
        // Regular expressions for APA format
        String authorPattern = properties.getProperty("authorPattern");
        String datePattern = properties.getProperty("datePattern");
        String titlePattern = properties.getProperty("titlePattern");
        String publisherPattern = properties.getProperty("publisherPattern");
        String locationPattern = properties.getProperty("locationPattern");


        String[] referenceParts = reference.split("\\. ");

        // Check author
        Pattern authorRegex = Pattern.compile(authorPattern);
        Matcher authorMatcher = authorRegex.matcher(referenceParts[0]);

        if (!authorMatcher.matches()) {
            return false;
        }

        // Check date
        Pattern dateRegex = Pattern.compile(datePattern);
        Matcher dateMatcher = dateRegex.matcher(referenceParts[1]);

        if (!dateMatcher.matches()) {
            return false;
        }

        // Check title
        Pattern titleRegex = Pattern.compile(titlePattern);
        Matcher titleMatcher = titleRegex.matcher(referenceParts[2]);

        if (!titleMatcher.matches()) {
            return false;
        }

        // Check publisher
        Pattern publisherRegex = Pattern.compile(publisherPattern);
        Matcher publisherMatcher = publisherRegex.matcher(referenceParts[3]);

        if (!publisherMatcher.matches()) {
            return false;
        }

        // Check location
        Pattern locationRegex = Pattern.compile(locationPattern);
        Matcher locationMatcher = locationRegex.matcher(referenceParts[4]);

        return locationMatcher.matches();
    }
}

